package com.company.hotel.configurer;

import com.company.hotel.shiro.filter.UserFormAuthenticationFilter;
import com.company.hotel.shiro.realm.CustomRealm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;


@Configuration
public class ShiroConfig {
    @Bean
    public ShiroFilterFactoryBean shirFilter(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        // 蹇呴』璁剧疆 SecurityManager
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        // setLoginUrl 濡傛灉涓嶈缃�硷紝榛樿浼氳嚜鍔ㄥ鎵網eb宸ョ▼鏍圭洰褰曚笅鐨�"/login.jsp"椤甸潰 鎴� "/login" 鏄犲皠
        shiroFilterFactoryBean.setLoginUrl("/login");
        // 璁剧疆鏃犳潈闄愭椂璺宠浆鐨� url;
        shiroFilterFactoryBean.setUnauthorizedUrl("/norole");
        // 璁剧疆鎷︽埅鍣�
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        //鐢ㄦ埛锛岄渶瑕佽鑹叉潈闄� 鈥渦ser鈥�
        filterChainDefinitionMap.put("/user/**", "roles[user]");
        //绠＄悊鍛橈紝闇�瑕佽鑹叉潈闄� 鈥渁dmin鈥�
        filterChainDefinitionMap.put("/admin/**", "roles[admin]");
        //寮�鏀剧櫥闄嗛〉闈�
        filterChainDefinitionMap.put("/login", "authc");
        //寮�鏀句富椤甸潰
        filterChainDefinitionMap.put("/index", "anon");
        //寮�鏀鹃潤鎬佹枃浠跺姞杞借矾寰�
        filterChainDefinitionMap.put("/static/**", "anon");
        //鍏朵綑鎺ュ彛涓�寰嬫嫤鎴�
        //涓昏杩欒浠ｇ爜蹇呴』鏀惧湪鎵�鏈夋潈闄愯缃殑鏈�鍚庯紝涓嶇劧浼氬鑷存墍鏈� url 閮借鎷︽埅
        filterChainDefinitionMap.put("/**", "authc");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        Map<String, Filter> filterMap = new LinkedHashMap<>();
        UserFormAuthenticationFilter authenticationFilter = new UserFormAuthenticationFilter();
        //璁剧疆filter
        filterMap.put("authc", authenticationFilter);
        shiroFilterFactoryBean.setFilters(filterMap);
        System.out.println("Shiro鎷︽埅鍣ㄥ伐鍘傜被娉ㄥ叆鎴愬姛");
        return shiroFilterFactoryBean;
    }

    /**
     * 娉ㄥ叆 securityManager
     */
    @Bean
    public DefaultWebSecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        // 
        securityManager.setRealm(customRealm());
        return securityManager;
    }

    /**
     * 鑷畾涔夎韩浠借璇� realm;
     * <p>
     * 蹇呴』鍐欒繖涓被锛屽苟鍔犱笂 @Bean 娉ㄨВ锛岀洰鐨勬槸娉ㄥ叆 CustomRealm锛�
     * 鍚﹀垯浼氬奖鍝� CustomRealm绫� 涓叾浠栫被鐨勪緷璧栨敞鍏�
     */
    @Bean
    public CustomRealm customRealm() {
        return new CustomRealm();
    }

    /**
     * 寮�鍚痵hiro娉ㄨВ
     * @return
     */
    @Bean
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        advisorAutoProxyCreator.setProxyTargetClass(true);
        return advisorAutoProxyCreator;
    }

    /**
     * 寮�鍚痑op娉ㄨВ鏀寔
     * @param securityManager
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }
    /**
     * 寮�鍚疭hiro鎺堟潈鐢熸晥
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(){
        return new AuthorizationAttributeSourceAdvisor();
    }
}
